//
//  ReciteViewController.m
//  Learn
//
//  Created by apple on 2024/4/1.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "ReciteViewController.h"
#import "PlanView.h"

#import "EditPlanViewController.h"
#import "WordListViewController.h"
#import "StartReciteViewController.h"
#import "PkViewController.h"

@interface ReciteViewController ()
@property (nonatomic, strong) UIImageView    * topBgImg;
@property (nonatomic, strong) UIButton    * startBtn;
@property (nonatomic, strong) UIButton    * pkBtn;
@property (nonatomic, strong) UIButton    * todayBgView;

/* bgview */
@property (nonatomic, strong) UIView    * bgView;
@property (nonatomic, strong) PlanView    * planTopView;
@property (nonatomic, strong) UIView    * lineView;
@property (nonatomic, strong) UILabel *planTitle;

@property (nonatomic, strong) UIButton    * greenBgView;

@property (nonatomic, strong) UIView    * proBgView;
@property (nonatomic, strong) UIView    * proView;
@property (nonatomic, strong) UILabel    * proLabel;

@property (nonatomic, strong) UIButton    * listBtn;
@property (nonatomic, strong) UIButton    * setPlanBtn;

@end

@implementation ReciteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.topBgImg];

    [self initCusNav];
    self.navigationBar.navTitle.text = @"背单词";
    self.navigationBar.navTitle.textColor = UIColor.whiteColor;

    self.navigationBar.bgColor = [UIColor.whiteColor colorWithAlphaComponent:0];
    [self.view addSubview:self.startBtn];
    [self.view addSubview:self.pkBtn];
    [self.view addSubview:self.todayBgView];

    [self.view addSubview:self.bgView];
    [self requestReciteData];
}
- (UIImageView *)topBgImg {
    if(!_topBgImg){
        _topBgImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W,kWIDTH_RATIO*277)];
        _topBgImg.contentMode = UIViewContentModeScaleAspectFill;
        _topBgImg.image = [UIImage imageNamed:@"planBg"];
    }
    return _topBgImg;
}


- (UIView *)bgView {
    if(!_bgView){
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(10, NavigationBarHeight + 20, SCREEN_W - 20, 381)];
        _bgView.backgroundColor = [UIColor whiteColor];
        [_bgView js_setRoundedCorners:UIRectCornerAllCorners radii:15];
        [_bgView addSubview:self.planTopView];
        [_bgView addSubview:self.greenBgView];
        [_bgView addSubview:self.proBgView];
        [_bgView addSubview:self.proView];
        [_bgView addSubview:self.proLabel];
        [_bgView addSubview:self.listBtn];
        [_bgView addSubview:self.setPlanBtn];

    }
    return _bgView;
}

- (PlanView *)planTopView {
    if(!_planTopView){
        _planTopView = [[PlanView alloc]initWithFrame:CGRectMake(0, 15, self.bgView.width, 185)];
        _planTopView.backgroundColor = [UIColor whiteColor];
        _planTopView.isHome = YES;
    }
    return _planTopView;
}

- (UIButton *)greenBgView {
    if(!_greenBgView){
        _greenBgView = [UIButton buttonWithType:UIButtonTypeCustom];
        _greenBgView.userInteractionEnabled = NO;
        _greenBgView.frame = CGRectMake(10, self.planTopView.bottom, self.bgView.width - 20, 28);
        
        [_greenBgView setImage:[UIImage imageNamed:@"clockIcon"] forState:UIControlStateNormal];
        [_greenBgView setTitle:@" 计划完成时间：2024-07-12" forState:UIControlStateNormal];
        
        [_greenBgView setTitleColor:COLOR_TOPIC_SELECT forState:UIControlStateNormal];
        _greenBgView.titleLabel.font  = PingFangMediumFont(12);
        
        _greenBgView.backgroundColor = [COLOR_TOPIC_SELECT colorWithAlphaComponent:0.1];
        [_greenBgView js_setRoundedCorners:UIRectCornerAllCorners radii:14];
    }
    return _greenBgView;
}



- (UIView *)proBgView {
    if(!_proBgView){
        _proBgView = [[UIView alloc]initWithFrame:CGRectMake(10, 30 + self.greenBgView.bottom, self.bgView.width - 20, 6)];
        
        _proBgView.backgroundColor = rgbHex(0xe5e5e5, 1);
        [_proBgView js_setRoundedCorners:UIRectCornerAllCorners radii:5];
    }
    return _proBgView;
}
- (UIView *)proView {
    if(!_proView){
        _proView = [[UIView alloc]initWithFrame:CGRectMake(self.proBgView.left, self.proBgView.top, 6, 6)];
        
        _proView.backgroundColor = COLOR_TOPIC_SELECT;
        [_proView js_setRoundedCorners:UIRectCornerAllCorners radii:5];
    }
    return _proView;
}

- (UILabel *)proLabel {
    if(!_proLabel){
        _proLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10 + self.proBgView.bottom, 200, 16)];
        _proLabel.right = self.bgView.width - 10;
        _proLabel.textColor = COLOR_NOT_SELECT;
        _proLabel.font = PingFangMediumFont(12);
        _proLabel.textAlignment = NSTextAlignmentRight;
    }
    return _proLabel;
}


- (UIButton *)listBtn {
    if(!_listBtn){
        _listBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _listBtn.frame = CGRectMake(10, self.proLabel.bottom + 30, 132, 32);
        _listBtn.right = self.bgView.width/2 - 15;
        [_listBtn setTitle:@"单词列表" forState:UIControlStateNormal];
        
        [_listBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        _listBtn.titleLabel.font  = PingFangMediumFont(12);
       
        [_listBtn js_setRoundedCorners:UIRectCornerAllCorners radii:18 borderColor:rgbHex(0xE5E5E5, 1) width:1];
        [_listBtn addTarget:self action:@selector(listBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _listBtn;
}
- (UIButton *)setPlanBtn {
    if(!_setPlanBtn){
        _setPlanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _setPlanBtn.frame = CGRectMake(10, self.proLabel.bottom + 30, 132, 32);
        _setPlanBtn.left = self.bgView.width/2 + 15;
        [_setPlanBtn setTitle:@"制定计划" forState:UIControlStateNormal];
        
        [_setPlanBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        _setPlanBtn.titleLabel.font  = PingFangMediumFont(12);
        [_setPlanBtn js_setRoundedCorners:UIRectCornerAllCorners radii:18 borderColor:rgbHex(0xE5E5E5, 1) width:1];

        [_setPlanBtn addTarget:self action:@selector(setPlanBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _setPlanBtn;
}




- (UIButton *)startBtn {
    if(!_startBtn){
        _startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _startBtn.frame = CGRectMake(10, 0, SCREEN_W/2 - 35/2, 45);
        _startBtn.bottom = SCREEN_H - 10 - SafeAreaBottomHeight;
        [_startBtn setTitle:@"开始背单词" forState:UIControlStateNormal];
        
        [_startBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        _startBtn.titleLabel.font  = PingFangMediumFont(18);
       
        [_startBtn js_setRoundedCorners:UIRectCornerAllCorners radii:22.5 borderColor:COLOR_NOT_SELECT width:1];
        [_startBtn addTarget:self action:@selector(startBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _startBtn;
}

- (UIButton *)pkBtn {
    if(!_pkBtn){
        _pkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _pkBtn.frame = CGRectMake(self.startBtn.right + 15, self.startBtn.top, SCREEN_W/2 - 35/2, 45);
        [_pkBtn setTitle:@"单词PK" forState:UIControlStateNormal];
        
        [_pkBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _pkBtn.titleLabel.font  = PingFangMediumFont(18);
        
        _pkBtn.az_colors = @[(__bridge id)rgbHex(0x02A1F9, 1).CGColor,(__bridge id)rgbHex(0x29F19C,1).CGColor];
        _pkBtn.az_startPoint = CGPointMake(0, 0.5);
        _pkBtn.az_endPoint = CGPointMake(1, 0.5);
        [_pkBtn js_setRoundedCorners:UIRectCornerAllCorners radii:22.5];
        [_pkBtn addTarget:self action:@selector(pkBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _pkBtn;
}

- (UIButton *)todayBgView {
    if(!_todayBgView){
        _todayBgView = [UIButton buttonWithType:UIButtonTypeCustom];
        _todayBgView.userInteractionEnabled = NO;
        _todayBgView.frame = CGRectMake(20, 0, SCREEN_W - 40 - 20, 28);
        _todayBgView.bottom = self.pkBtn.top - 15;
        [_todayBgView setImage:[UIImage imageNamed:@"tipIcon"] forState:UIControlStateNormal];
        _todayBgView.titleLabel.font  = PingFangMediumFont(12);
        
        _todayBgView.backgroundColor = [COLOR_TOPIC_SELECT colorWithAlphaComponent:0.1];
       
        [_todayBgView js_setRoundedCorners:UIRectCornerAllCorners radii:14];
    }
    return _todayBgView;
}

#pragma mark   设置计划
- (void)setPlanBtnClicked:(UIButton *)sender {
    EditPlanViewController *editPlanVC = [[EditPlanViewController alloc]init];
    editPlanVC.model = self.model;
    WS(WeakSelf)
    editPlanVC.editPlanBlock = ^{
        [WeakSelf requestReciteData];
    };
    [self.navigationController pushViewController:editPlanVC animated:YES];
}
#pragma mark   单词列表
- (void)listBtnClicked:(UIButton *)sender {
    WordListViewController *listVc = [[WordListViewController alloc]init];
    [self.navigationController pushViewController:listVc animated:YES];
}

#pragma mark   开始背单词
- (void)startBtnClicked:(UIButton *)sender {
    StartReciteViewController *startVC = [[StartReciteViewController alloc]init];
    startVC.versionID = self.model.version_full_name_id;
    [self.navigationController pushViewController:startVC animated:YES];
}

#pragma mark   pk
- (void)pkBtnClicked:(UIButton *)sender {
    PkViewController *pkVC = [PkViewController new];
    [self.navigationController pushViewController:pkVC animated:YES];
}


- (void)updateUIWithModel:(ReciteModel *)model {
    self.model = model;
    [self.planTopView setTitle:model.version_full_name num:model.words_count];
    [self.planTopView updatePlanDay:model.end_day num:model.plan_count];
    [self planEndTimeWithDays:model.end_day];
    [self setYiXueWanNum:model.yes_count allNum:model.words_count];
    [self setNeedNum:model.today_count planNum:model.plan_count fuxiNum:model.today_count];
}
#pragma mark   request
- (void)requestReciteData {
    WS(WeakSelf)
    [BeiDanCiRequest request_getReciteDataResult:^(ReciteModel * _Nonnull model, BOOL isDone) {
        if (isDone) {
            [WeakSelf updateUIWithModel:model];
        }
    }];
}

- (void)planEndTimeWithDays:(NSString *)day{
    NSDate *current = [NSDate date];
    int dayNum = day.intValue;
    NSDate *endDate ;
    NSTimeInterval oneDay = 24*60*60;
    endDate = [current initWithTimeIntervalSinceNow:oneDay*dayNum];
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setDateFormat:@"yyyy-MM-dd"];
    NSString * time = [NSString stringWithFormat:@"%@ ",[_formatter stringFromDate:endDate]];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.greenBgView setTitle:[NSString stringWithFormat:@" 计划完成时间：%@",time]  forState:UIControlStateNormal];
    });
}

//[self setNeedNum:@"0" fuxiNum:@"0"];

- (void)setYiXueWanNum:(NSString *)num allNum:(NSString *)allNum {
    CGFloat leftLength = 0;
    
    NSString *strt = @"已学完 ";
    NSString *leftNum = @"0";
    NSString *rightNum = @"/888";
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@",strt,leftNum,rightNum]];
    [attributeStr addAttribute:NSForegroundColorAttributeName
                         value:COLOR_NOT_SELECT
                         range:NSMakeRange(leftLength, strt.length)];
    leftLength += strt.length;
    
    [attributeStr addAttribute:NSForegroundColorAttributeName
                         value:COLOR_TOPIC_SELECT
                         range:NSMakeRange(leftLength, leftNum.length)];
    [attributeStr addAttribute:NSFontAttributeName value:PingFangMediumFont(16) range:NSMakeRange(leftLength, leftNum.length)];
    leftLength += leftNum.length;
    
    [attributeStr addAttribute:NSForegroundColorAttributeName
                         value:COLOR_BLACK
                         range:NSMakeRange(leftLength,rightNum.length )];
    self.proLabel.attributedText = attributeStr;
}
- (void)setNeedNum:(NSString *)studyNum planNum:(NSString *)planNum fuxiNum:(NSString *)fuxiNum {
    CGFloat leftLength = 0;
    
    NSString *str1 = @" 今日需新学";
    NSString *num1 = [NSString stringWithFormat:@"%@",studyNum];
    NSString *num2 = [NSString stringWithFormat:@"/%@",planNum];
    NSString *str2 = @"   今日需复习";

    NSString *num3 = [NSString stringWithFormat:@"%@",fuxiNum];

    
    
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@%@%@",str1,num1,num2,str2,num3]];
    [attributeStr addAttribute:NSForegroundColorAttributeName
                         value:COLOR_BLACK
                         range:NSMakeRange(leftLength, str1.length)];
    leftLength += str1.length;
    
    [attributeStr addAttribute:NSForegroundColorAttributeName
                         value:COLOR_TOPIC_SELECT
                         range:NSMakeRange(leftLength, num1.length)];
    [attributeStr addAttribute:NSFontAttributeName value:PingFangMediumFont(16) range:NSMakeRange(leftLength, num1.length)];
    leftLength += num1.length;
    
    [attributeStr addAttribute:NSForegroundColorAttributeName
                         value:COLOR_BLACK
                         range:NSMakeRange(leftLength,num2.length )];
    leftLength += num2.length;

    [attributeStr addAttribute:NSForegroundColorAttributeName
                         value:COLOR_BLACK
                         range:NSMakeRange(leftLength,str2.length )];
    leftLength += str2.length;

    [attributeStr addAttribute:NSForegroundColorAttributeName
                         value:COLOR_TOPIC_SELECT
                         range:NSMakeRange(leftLength, num3.length)];
    [attributeStr addAttribute:NSFontAttributeName value:PingFangMediumFont(16) range:NSMakeRange(leftLength, num3.length)];
    leftLength += num3.length;
    
    [self.todayBgView setAttributedTitle:attributeStr forState:UIControlStateNormal];
}

@end
